package com.etcn.common.jwt;

import java.security.Key;
import java.util.Date;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.etcn.modules.busi.member.pojo.WmMember;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

@Component
public class JwtUtil {

	private Logger logger = LoggerFactory.getLogger(JwtUtil.class);

	@Autowired
	private JwtConfig jwtConfig;

	public Claims parseJWT(String jsonWebToken, String base64Security) {
		try {
			Claims claims = Jwts.parser().setSigningKey(DatatypeConverter.parseBase64Binary(base64Security))
					.parseClaimsJws(jsonWebToken).getBody();
			return claims;
		} catch (ExpiredJwtException e) {
			logger.error("token已失效", e);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	public String createJWT(WmMember member, String base64Security, String timestamp) {
		SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
		// 生成签名密钥
		byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(base64Security);
		Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
		// 添加构成JWT的参数
		JwtBuilder builder = Jwts.builder().setId(member.getMemberId()).setSubject(timestamp)
				.signWith(signatureAlgorithm, signingKey)
				.setExpiration(new Date(System.currentTimeMillis() + jwtConfig.getExpiration()))
				.setNotBefore(new Date(System.currentTimeMillis()));
		// 生成JWT
		return builder.compact();
	}

}
